from copy import Error
import csv
import urllib3
from selenium import webdriver
import bs4
import re
from collections import namedtuple
import random

if __name__ == "__main__":
    options = webdriver.ChromeOptions()
    prefs = {
        'profile.default_content_setting_values': {
            'images': 2,
        }
    }
    options.add_experimental_option('prefs', prefs)
    options.add_argument('--headless')
    driver = webdriver.Chrome(options=options)
    
    HotelInfo = namedtuple("HotelInfo", ["id", "name","image","price","location", "score"])
    
    with open("hotel_spyder/cq.csv", 'r') as f:
        with open('output.csv', 'w') as output_f:
            output_f.write("id,name,image,price,location, score\n")
            writer = csv.writer(output_f)
            id_reader = csv.reader(f.readlines())
            for line in id_reader:
                for id in line:
                    try :
                    
                        print(id)
                        driver.get( "https://hotel.meituan.com/"+id)
                        html = driver.find_element_by_xpath(
                            "//*").get_attribute("outerHTML")
                        soup = bs4.BeautifulSoup(html, features="lxml")
                        
                        # name
                        name = soup.find(class_="fs26 fc3 pull-left bold").string
                        
                        print(name)
                        
                        # image
                        img = str(soup.find(class_="pc-album-main pc-album-common-img"))
                        # print(img)
                        img = re.search(r"url\(.+\)", img)
                        img = img.group(0)[5:-2]
                        print(img)
                        
                        # price
                        
                        # location
                        location = soup.find(class_="fs12 mt6 mb10")
                        location = location.span.string
                        print(location)
                        
                        # score
                        score = soup.find(class_="fs34")
                        score = score.string
                        print(score)
                        
                        hotel = HotelInfo(id, name, img, random.randint(200, 800), location, score)
                        
                        writer.writerow(hotel)
                    except :
                        print("except")
                    
                    
                    
                
